package com.hzit.ms.controller;

import com.hzit.ms.resp.RespVo;
import com.hzit.ms.resp.UserData;
import com.hzit.ms.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;



    /**
     * 加载登录界面
     * @return
     */
    @RequestMapping("show")
    public String show(){
        logger.info("##########加载登录页面##########");
        return "login";
    }

    /**
     * 登录接口 返回json
     */
    @ResponseBody
    @RequestMapping("/login")
    public RespVo<UserData> userLogin(@RequestParam("mobile") String mobile,
                                      @RequestParam("password") String password){

        logger.info("接收到到手机号：{}的登录请求密码：{}",mobile,password);
        RespVo respVo = new RespVo();

        if (StringUtils.isEmpty(mobile)){
            respVo.setCode(-1);
            respVo.setMsg("手机号不能为空");
            return respVo;
        }

        if (StringUtils.isEmpty(password)){
            respVo.setCode(-1);
            respVo.setMsg("密码不能为空");
            return respVo;
        }

        //执行登录
        respVo = userService.userLogin(mobile,password);

        return respVo;

    }
}
